<template>
	<view class="login login-index">
		<view class="body position-center">
			<view class="icon">
				<image src="https://img1.baidu.com/it/u=4279899799,581033126&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1717606800&t=ad2249b3b2154167bff6366662685f7c" 
					   mode="">
				</image>
			</view>
			<view class="button">
				<view class="btn-body" @click="login">
					使用微信一键登录
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { BASE_IMAGE_URL } from '../../../utils/CommonValues.js';
	import request from '../../../utils/request.js';
	export default {
		data() {
			return {
				userInfo: {
					memberName: '',
					memberImg: '',
					weOpenid: '',
					memberAccount: '',
					parentAccount: 0,
				}
			}
		},
		onLoad() {
			uni.getStorage({
				key: 'parent_account',
				success: (response) => {
					if(response.data) {
						this.parentAccount = response.data;
						// uni.showToast({
						// 	title: response.data
						// })
						// 如果获取的数据为null或undefine, 就设置为自主进入
						if(!this.parentAccount) {
							this.parentAccount = 'AWP';
						}
					}
				},
				fail: (e) => {
					// AWP 为标志信息, 表示当前进入小程序的方式为自主进入, 而不是他人分享
					this.parentAccount = 'AWP';
				}
			})
		},
		methods: {
			// 发送登录请求到后台
			submitLogin() {
				request.POST('/system/members/app_login', this.userInfo).then(response => {
					console.log(response);
					if(response.data) {
						var userInfo = response.data;
						// 把信息存入 Storage
						uni.setStorage({
							key: 'app_user_info',
							data: JSON.stringify(userInfo)
						})
						uni.showToast({
							title: response.message,
							icon: 'error'
						})
						uni.navigateBack();
					} else {
						uni.showToast({
							title: response.message,
							icon: 'error'
						})
					}
				}).catch(error => {
					console.log(error);
					uni.showToast({
						title: '登录信息异常',
						icon: 'error'
					})
				})
			},
			// 登录
			login() {
				uni.showToast({
					title: '登录中',
					icon: 'loading',
				})
				uni.getUserInfo({
					success: (userInfo) => {
						this.userInfo.memberName = userInfo.userInfo.nickName;
						this.userInfo.memberImg = userInfo.userInfo.avatarUrl;
						if(this.parentAccount != 'AWP'){
							// 如果不是自主进入, 那么上级id就要设置成分享的连接中的id
							// uni.showToast({
							// 	title: this.parentAccount
							// })
							this.userInfo.parentAccount = this.parentAccount;
						}
						console.log('用户信息：', this.userInfo)
						//使用uni.login的时候可以在任何接口下使用即可，主要看打印出来的code值和openid
						let that = this
						uni.login({
						    provider: 'weixin',
						    success: (loginRes) => {
								console.log('用户信息', loginRes)
								this.getOpenid(loginRes.code).then(res=>{
									this.userInfo.weOpenid = res.data.openid;
									this.userInfo.memberAccount = res.data.openid;
									console.log('openId', res.data.openid)
									this.submitLogin();
								})
						    },
							fail() {
								uni.showToast({
									title: '获取openid失败',
									icon: 'error',
								})
							}
						});
					},
					fail() {
						uni.showToast({
							title: '获取微信用户信息失败',
							icon: 'error',
						})
					}
				})
			},
			// 获取微信openid
			getOpenid(code) {
			    // return new Promise(function (resolve, reject) {
			    //     let appid = 'wx05b9603b56afcc60'
			    //     let secret = '72aa1913a1cd99b3ce3c6f17ca73a2a3'
			    //     let url = `https://api.weixin.qq.com/sns/jscode2session?appid=${appid}&secret=${secret}&js_code=${code}&grant_type=authorization_code`
			    //     wx.request({
			    //         url: url,
			    //         success: (res) => {
							// console.log(res, 'openid');
			    //             let openid = res.data;
			    //             resolve(openid)
			    //         }
			    //     })
			    // })
				return request.GET(`/wx/openid/${code}`, null);
			}
		}
	}
</script>
<style lang="less">
.login {
	position: relative;
	width: 750rpx;
	height: 100vh;
	.body {
		width: 750rpx;
		.icon{
			width: 180px;
			height: 100px;
			margin: 30px auto;
		}
		.button {
			width: 750rpx;
			height: 50px;
			.btn-body {
				width: 700rpx;
				height: 40px;
				line-height: 40px;
				color: white;
				font-weight: bold;
				margin: 0 auto;
				text-align: center;
				border-radius: 5px;
				background-color: seagreen;
				&:active {
					background-color: forestgreen;
				}
			}
		}
	}
}
</style>
